import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class BasicCalculator extends JFrame{
	private JTextField textField1;
	private JTextField textField2;
	private JTextArea textArea;
	private String result;
	
	public BasicCalculator(){
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		panel.setLayout(new GridLayout(10, 10, 10, 10));
		
		textField1  = new JTextField();
		textField2 = new JTextField();
		textArea = new JTextArea();
		JLabel label1 = new JLabel("Field1");
		JLabel label2 = new JLabel("Field2");
		JLabel label3 = new JLabel("Result");
//		JButton submitButton = new JButton("Submit");
		JButton clearButton = new JButton("Clear");
		final JButton addButton = new JButton("+");
		final JButton divideButton = new JButton("/");
		final JButton multiplyButton = new JButton("*");
		final JButton subtractButton = new JButton("-");
		
		textArea.setBorder(BorderFactory.createLineBorder(getBackground(), NORMAL));
		textArea.setEditable(false);
		panel.add(label1);
		panel.add(textField1);
		panel.add(label2);
		panel.add(textField2);
		panel.add(label3);
		panel.add(textArea);
		panel.add(addButton);
		panel.add(subtractButton);
		panel.add(divideButton);
		panel.add(multiplyButton);
//		panel.add(submitButton);
		panel.add(clearButton);
		
		add(panel);
		setTitle("Basic Calculator");
		setSize(getPreferredSize());
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
//		setVisible(true);
		
/*		submitButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				textArea.setText(null);
				
			}
		});
*/		
		clearButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				textArea.setText(null);
				textField1.setText(null);
				textField2.setText(null);
			}
		});
		
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				addButton.setActionCommand("+");
				result = String.valueOf(compute(addButton));
				textArea.setText(result);
			}
		});
		
		divideButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				divideButton.setActionCommand("/");
				result = String.valueOf(compute(divideButton));
				textArea.setText(result);
			}
		});
		
		subtractButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				subtractButton.setActionCommand("-");
				result = String.valueOf(compute(subtractButton));
				textArea.setText(result);
			}
		});
		
		multiplyButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				multiplyButton.setActionCommand("*");
				result = String.valueOf(compute(multiplyButton));
				textArea.setText(result);
			}
		});
	}
	
	public int compute(JButton button){
		if (button.getActionCommand().equalsIgnoreCase("+")){
			return Integer.parseInt(textField1.getText()) + Integer.parseInt(textField2.getText()); 
		}
		else if (button.getActionCommand().equalsIgnoreCase("-")){
			return Integer.parseInt(textField1.getText()) - Integer.parseInt(textField2.getText()); 
		}
		else if (button.getActionCommand().equalsIgnoreCase("/")){
			return Integer.parseInt(textField1.getText()) / Integer.parseInt(textField2.getText()); 
		}
		
		else if (button.getActionCommand().equalsIgnoreCase("*")){
			return Integer.parseInt(textField1.getText()) * Integer.parseInt(textField2.getText()); 
		}
		return 0;
	}

}
